package cn.itcast.wushangxiantu.BaseAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.UserData.ZhanShanYulan;

public class ZhanShanYuLanAdatper extends BaseAdapter {
    private Context context;
    private ZhanShanYulan zhanShanYulan;

    public ZhanShanYuLanAdatper(Context context) {
        this.context = context;
    }

    public ZhanShanYulan getZhanShanYulan() {
        return zhanShanYulan;
    }

    public void setZhanShanYulan(ZhanShanYulan zhanShanYulan) {
        this.zhanShanYulan = zhanShanYulan;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return zhanShanYulan.getZhanshan().size();
    }

    @Override
    public Object getItem(int i) {
        return zhanShanYulan.getZhanshan().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view=LayoutInflater.from(context).inflate(R.layout.adapter_zhanshanyulan, null);
        ViewHolder viewHolder=new ViewHolder(view);
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newTime = formatter.format(currentTime); // 当前本地时间
        long OTime = 0;
        long NTime=0;
        try {
            OTime = formatter.parse(zhanShanYulan.getZhanshan().get(i).getTime()).getTime();
            NTime = formatter.parse(newTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = (NTime - OTime);
        if (diff < 2400000&&zhanShanYulan.getZhanshan().get(i).getTime().length()>10) {
            viewHolder.tvBaohu.setText("保护中");
            viewHolder.tvBaohu.setTextColor(Color.RED);
        }else {
            viewHolder.tvBaohu.setText("可占领");
            viewHolder.tvBaohu.setTextColor(Color.BLUE);
        }


        if (zhanShanYulan.getZhanshan().get(i).getUsernumber().trim().equals("")||zhanShanYulan.getZhanshan().get(i).getUsernumber().trim()==null){
            viewHolder.tvHuShanName.setText("无拥有者");
            viewHolder.tvHuShanName.setTextColor(Color.GRAY);
        }else{
            viewHolder.tvHuShanName.setText("拥有者："+zhanShanYulan.getZhanshan().get(i).getUsername());
            viewHolder.tvDengJi.setText("等级："+zhanShanYulan.getZhanshan().get(i).getuserdj());
        }
        viewHolder.tvName.setText(zhanShanYulan.getZhanshan().get(i).getDj() + zhanShanYulan.getZhanshan().get(i).getPz() + zhanShanYulan.getZhanshan().get(i).getId() + "号山");
        return view;
    }

    class ViewHolder {
        public View rootView;
        public TextView tvName;
        public TextView tvHuShanName;
        public TextView tvBaohu;
        public TextView tvDengJi;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvHuShanName = (TextView) rootView.findViewById(R.id.tvHuShanName);
            this.tvBaohu = (TextView) rootView.findViewById(R.id.tvBaohu);
            this.tvDengJi=(TextView)rootView.findViewById(R.id.tvDengJi);
        }

    }
}
